﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using BUS;
using System.Data.SqlClient;
namespace QLDT
{
    public partial class Login : DevExpress.XtraEditors.XtraForm
    {
        private SqlConnection conn;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";
        DTO_TaiKhoan taikhoan = new DTO_TaiKhoan();
        public Login()
        {
            InitializeComponent();
        }
        public void dangnhap()
        {
            List<DTO_TaiKhoan> tk = new List<DTO_TaiKhoan>();
            
            tk = BUS.BUS_TaiKhoan.DangNhap(txtTenTK.Text, txtMatKhau.Text);
            if (tk.Count > 0)
            {
               
               
                DTO_TaiKhoan taikhoan = new DTO_TaiKhoan();
                taikhoan = tk[0];
                frm_main frm = new frm_main(taikhoan);
                frm.Show();
                this.Hide();
            }
            else
                XtraMessageBox.Show("Đăng nhập không hợp lệ", "Thông báo");
        }
        private void simpleBt_DangNhap_Click(object sender, EventArgs e)
        {
            dangnhap();
        }

        private void txtTenTK_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtMatKhau.Focus();
        }

        private void txtMatKhau_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                dangnhap();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            txt_datasource.Focus();
            group1.Visible = false;
            group2.Visible = true;
            txt_datasource.Text = "WIN-99CBLSA44R4";
            txt_user.Text = "sa";
            txt_password.Text = "00000";
            txtTenTK.Text = "2111110188";
            txtMatKhau.Text = "101093";
            
            simpleBt_login.Enabled = false;
        }
        public void connect()
        {
            try
            {
                connectionString = "Data Source=" + txt_datasource.Text + ";User Id=" + txt_user.Text + ";Password=" + txt_password.Text + ";";
                conn = new SqlConnection(connectionString);
                conn.Open();
                sql = "EXEC sp_databases";
                command = new SqlCommand(sql, conn);
                reader = command.ExecuteReader();
                cmb_database.Items.Clear();
                while (reader.Read())
                {
                    cmb_database.Items.Add(reader[0].ToString());
                }
                txt_datasource.Enabled = false;
                txt_password.Enabled = false;
                txt_user.Enabled = false;
                simpleBt_ketnoi.Enabled = false;
                simpleBt_Huyketnoi.Enabled = true;
                cmb_database.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void simpleBt_connect_Click(object sender, EventArgs e)
        {
            connect();
        }

        private void simpleBt_Huyketnoi_Click(object sender, EventArgs e)
        {
            txt_datasource.Enabled = true;
            txt_user.Enabled = true;
            txt_password.Enabled = true;
            cmb_database.Enabled = false;
            simpleBt_ketnoi.Enabled = true;
            simpleBt_DangNhap.Enabled = false;

        }

        private void simpleBt_login_Click(object sender, EventArgs e)
        {
                group1.Visible = true;
        }

        private void cmb_database_SelectedIndexChanged(object sender, EventArgs e)
        {
            simpleBt_login.Enabled = true;
        }

        private void simpleBt_tuychon_Click(object sender, EventArgs e)
        {
            group2.Visible = true;
        }

        private void simpleBt_Thoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txt_datasource_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                connect();
                dangnhap();
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void group1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}